package com.financing.wap.web.controller.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.dto.YundaHefeiDTO;
import com.financing.wap.service.IYundaHefeiService;
import com.financing.wap.util.SecureUtil;
import com.financing.wap.web.controller.BaseController;
import com.financing.wap.weixin.util.SignUtil;

@Controller("yundaHefeiController")
@RequestMapping("/api/yundaHefei")
public class YundaHefeiController extends BaseController  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IYundaHefeiService yundaHefeiService;
	
	@RequestMapping("/listYundaHefei")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, YundaHefeiDTO yundaHefei,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query yundaHefei list begin .......");
			
			yundaHefei.setBegin(pager.getStartIndex());
			yundaHefei.setPageSize(pager.getPageSize());
			
			int count = yundaHefeiService.queryListCount(yundaHefei);
			
			pager.setTotalCount(count);
			
			List<YundaHefeiDTO> yundaHefeiList = null;
			
			if(count > 0){
				yundaHefeiList = yundaHefeiService.queryList(yundaHefei);
			}else{
				yundaHefeiList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", yundaHefeiList);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query yundaHefei list end .......");
		}catch(Exception e){
			log.error("query yundaHefei error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/addYundaHefei")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, YundaHefeiDTO yundaHefei){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add yundaHefei  begin .......");

			
			int id = yundaHefeiService.insert(yundaHefei);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add yundaHefei list end .......");
		}catch(Exception e){
			log.error("add yundaHefei error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/queryByMobile")
	@ResponseBody
	public ResultMap queryByMobile(HttpServletRequest request){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail yundaHefei  begin .......");
			
			String telPhone = request.getParameter("telno");
			String secretNoid=request.getParameter("noid");
			
			if(StringUtils.isBlank(telPhone)||StringUtils.isBlank(secretNoid)){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			String noid=SecureUtil.decrypt(secretNoid);
			
			YundaHefeiDTO yundaHefei =new YundaHefeiDTO();
			yundaHefei.setNoid(Integer.parseInt(noid));
			YundaHefeiDTO dto = yundaHefeiService.queryListGetFirst(yundaHefei);
			
			if(dto!=null&&dto.getId()!=null){
				if(dto.getErrorCount()!=null&&dto.getErrorCount()>=3){
					result.setStatusCode(ResultMap.MAX_ERROR);
					result.setMessage("输入错误超过3次，锁定");
					result.setStatus(ResultMap.STATUS.FAILE);
					return result;
				}
				if(!dto.getTelno().equals(telPhone)){
					int errCount=(dto.getErrorCount()==null?0:dto.getErrorCount())+1;
					dto.setErrorCount(errCount);
					yundaHefeiService.update(dto);
					result.setAttribute("errorCount",errCount);
					result.setStatusCode(ResultMap.TELNO_ERROR);
					result.setMessage("手机号不匹配");
					result.setStatus(ResultMap.STATUS.FAILE);
					return result;
				}else{
					dto.setErrorCount(0);
					yundaHefeiService.update(dto);
				}
				result.setAttribute("dto",dto);
			}
		
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail yundaHefei  end .......");
		}catch(Exception e){
			log.error("query detail yundaHefei error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/detailYundaHefei")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, YundaHefeiDTO yundaHefei){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail yundaHefei  begin .......");
			
			Integer id = yundaHefei.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			YundaHefeiDTO dto = yundaHefeiService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail yundaHefei  end .......");
		}catch(Exception e){
			log.error("query detail yundaHefei error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteYundaHefei")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, YundaHefeiDTO yundaHefei){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete yundaHefei  begin .......");
			
			Integer id = yundaHefei.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			yundaHefeiService.delete(id);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete yundaHefei  end .......");
		}catch(Exception e){
			log.error("delete yundaHefei error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateYundaHefei")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, YundaHefeiDTO yundaHefei){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update yundaHefei  begin .......");
			
			Integer id = yundaHefei.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			
			yundaHefeiService.update(yundaHefei);
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update yundaHefei list end .......");
		}catch(Exception e){
			log.error("update yundaHefei error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceYundaHefei")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, YundaHefeiDTO yundaHefei){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce yundaHefei  begin .......");
			
			Integer id = yundaHefei.getId();
			
			if(id==null){
				result.setStatusCode(ResultMap.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			
			yundaHefeiService.updateForce(yundaHefei);
			
			
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce yundaHefei list end .......");
		}catch(Exception e){
			log.error("updateForce yundaHefei error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}
	
}
